package top.gohigh.commonview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;

import java.util.Timer;
import java.util.TimerTask;


/**
 * 自定义搜索按钮
 * 文字前方添加旋转动画
 * 测试机型 MI 6
 * 不确定是否适合其他情况
 * 添加theme属性改变样式
 * create by fupan 2018-12-30 21:32:24
 */
public class GHSearchBtn extends androidx.appcompat.widget.AppCompatButton {


    private Timer timer;
    private boolean validate=true;
    private TimerTask task;

    private int count=0;
    private int maxCount=1000;

    private String defStr;
    private String actStr="正在搜索";

    private int outsideArcColor = Color.rgb(157,211,250);
    private int insideArcColor =Color.rgb(31,111,181);

    //初始位置
    private float oStartPos=-90f;
    private float iStartPos=70f;

    //两段弧长
    private float olen=280f;
    private float ilen=60f;
    //每次变化角度
    private float odelta=8f;
    private float idelta=-8f;
    //每次真实起始角度
    private float oTPos=0f;
    private float iTPos=0f;

    //笔刷宽度
    private int oWidth=10;
    private int iWidth=10;

    //相当于外圈内圈半径
    private int oSize=35;
    private int iSize=15;

    //动画中心偏移
    private int centerOffset=-160;

    private Paint mPaint=new Paint();
    private Point mCenter;
    private RectF mOval,mOval2;


    public GHSearchBtn(Context context) {
        super(context);
        init();
    }

    public GHSearchBtn(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public GHSearchBtn(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    private void init(){
        defStr=getText().toString();

    }

    /**
     * 搜索停止
     * 设置为可以搜索状态
     */
    public void searchStopped(){
        validate=true;
        setText(defStr);
       timer.cancel();
       task.cancel();
       count=0;
       invalidate();
    }

    /**
     * 搜索开始
     * 设置为不可搜索状态
     */
    public void searchStarted(){
        if(validate==false)return;
        timer=new Timer();
        task=new TimerTask() {
            @Override
            public void run() {
             //   Log.v("mylog","搜索按钮 timer tick");
                count++;
                if(count==maxCount)count=0;

                oTPos=oStartPos+odelta*count;
                iTPos=iStartPos+idelta*count;

                invalidate();
            }
        };

        setText(actStr);
        validate=false;
        timer.schedule(task,0,20);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //返回true 截获事件
        super.onTouchEvent(event);
        return validate;

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);


       if(!validate){

            //开始自定义绘图
            Paint paint=mPaint;
            //首先绘制最外层的圆
            paint.setAntiAlias(true);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(oWidth);
            paint.setStrokeJoin(Paint.Join.ROUND);
            paint.setStrokeCap(Paint.Cap.ROUND);
            paint.setColor(outsideArcColor);
            //动画中心
           if(mCenter==null){
               mCenter=new Point(getWidth()/2+centerOffset,getHeight()/2);
               mOval=new RectF(mCenter.x-oSize,mCenter.y-oSize,mCenter.x+oSize,mCenter.y+oSize);
               mOval2=new RectF(mCenter.x-iSize,mCenter.y-iSize,mCenter.x+iSize,mCenter.y+iSize);
           }
            canvas.drawArc(mOval,oTPos,olen,false,paint);
            paint.setColor(insideArcColor);
            paint.setStrokeWidth(iWidth);
            canvas.drawArc(mOval2,iTPos,ilen,false,paint);

        }



    }


}
